import 'package:logger/logger.dart';

enum LogType {
  all,
  debug,
  info,
  warning,
  error,
  fatal,
  off,
}

/// 日志打印
/// ```dart
/// LogUtil.instance.log()
/// // 或
/// LogUtil().log()
/// ```
class LogManager {
  factory LogManager() => _getInstance();
  static LogManager get instance => _getInstance();

  static LogManager? _instance;
  static LogManager _getInstance() {
    _instance ??= LogManager._internal();
    return _instance!;
  }

  LogManager._internal() {
    _logger = Logger(printer: PrettyPrinter(methodCount: 0));
  }

  late Logger _logger;

  void log(dynamic obj, {LogType type = LogType.debug}) {
    Level level = _getLevelForLogLevel(type);
    _logger.log(level, obj);
  }

  Level _getLevelForLogLevel(LogType type) {
    switch (type) {
      case LogType.all:
        return Level.all;
      case LogType.debug:
        return Level.debug;
      case LogType.info:
        return Level.info;
      case LogType.warning:
        return Level.warning;
      case LogType.error:
        return Level.error;
      case LogType.fatal:
        return Level.fatal;
      case LogType.off:
        return Level.off;
      default:
        return Level.debug;
    }
  }
}
